package com.gitee.wsl.ext.array

import kotlin.math.pow
import kotlin.math.sqrt


val DoubleArray.stdDeviation: Double
    get() {
        if (isEmpty()) return 0.0

        //val mean = mean(seq)
        val sumOfSquaredDistances = sumOf { (it - mean).pow(2) }
        return sqrt(sumOfSquaredDistances / size)
    }

val FloatArray.stdDeviation: Double
    get() {
        if (isEmpty()) return 0.0

        //val mean = mean(seq)
        val sumOfSquaredDistances = sumOf { (it - mean).pow(2) }
        return sqrt(sumOfSquaredDistances / size)
    }

val IntArray.stdDeviation: Double
    get() {
        if (isEmpty()) return 0.0

        //val mean = mean(seq)
        val sumOfSquaredDistances = sumOf { (it - mean).pow(2) }

        return sqrt(sumOfSquaredDistances / size)
    }